Skip to content

fix(grok): accept and strip Anthropic cache_control on tools and tool blocks - #37

Merged
raine merged 1 commit into
raine:mainfrom
wtfsayo:grok-strip-anthropic-fields
Jul 12, 2026
Merged

fix(grok): accept and strip Anthropic cache_control on tools and tool blocks#37
raine merged 1 commit into
raine:mainfrom
wtfsayo:grok-strip-anthropic-fields

Conversation

@wtfsayo

@wtfsayo wtfsayo commented Jul 12, 2026

Copy link
Copy Markdown
Contributor

Problem

Claude Code inserts prompt-cache breakpoints (cache_control: {type: "ephemeral"}) once a session has enough context, and it attaches those markers to tool definitions and to tool_use / tool_result blocks. On a fresh single-turn chat you never see them, so the happy path looks fine.

The failure shows up multi-turn: after the agent has run a tool or two, every following turn replays the accumulated tool history with cache_control markers attached. The current Grok translation already accepts cache_control on system, text, and top-level tool_result, but hard-rejects it in three spots that only appear once a tool loop is underway:

  • tool definitionsunsupported tool field: cache_control
  • tool_use blocksobject.len() != 4
  • tool_result text childrenpart.len() != 2

So a session that works on turn one starts returning 400 the moment tools + caching breakpoints are in play (and reliably when switching to Grok mid-session). The marker is a client-side caching hint that upstream never needs to see.

Fix

  • parse_tools: allow cache_control on tool definitions and validate it via the existing valid_cache_control helper.
  • tool_use blocks: allow an optional cache_control key instead of requiring exactly 4 keys.
  • tool_result text children: allow an optional cache_control key instead of requiring exactly {type, text}.

In all three cases the marker is validated and then dropped; nothing new is forwarded to the Responses API. This mirrors how system / text / top-level tool_result already handle it.

Testing

  • 3 new unit tests: accept + strip on tools, accept + strip on tool_use / tool_result children, reject malformed cache_control.
  • cargo test --all (503 passed), cargo fmt --check, and clippy clean.

@wtfsayo wtfsayo changed the title grok: accept and strip Anthropic cache_control on tools, tool_use, and tool_result children fix(grok): accept and strip Anthropic cache_control on tools and tool blocks Jul 12, 2026
…d tool_result children

Claude Code attaches ephemeral prompt-cache markers (cache_control) to tool
definitions, tool_use blocks, and tool_result text children mid-tool-loop.
The current translation hard-rejected these (e.g. "unsupported tool field:
cache_control"), causing 400s when switching to Grok mid-session. Accept and
validate the marker (matching existing system/text/tool_result handling) and
drop it before forwarding to the upstream Responses API.
@wtfsayo
wtfsayo force-pushed the grok-strip-anthropic-fields branch from 9e78701 to f106b3d Compare July 12, 2026 09:58
@raine
raine merged commit 3aac922 into raine:main Jul 12, 2026
@raine

raine commented Jul 12, 2026

Copy link
Copy Markdown
Owner

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants